import mcht from '@/api/mcht'
import { defineStore } from 'pinia'

export const useHomeStore = defineStore('home', () => {
  const mchtNo = ref('')
  const shopId = ref('')
  const userName = ref('')
  const roleFlag = ref('')
  const notice = ref([])
  const phoneNumber = ref('')
  const digitalCurrency = ref({})
  const discount = ref({})
  const transactionSummary = ref({})
  const msgSwitch = ref<string>('0')
  const menuBit = ref('')
  const bit8 = ref('')
  const bit10 = ref('')
  const adjustFlag = ref<string>('0')
  const sb = ref<string>('0')
  const modifyRefund = ref<any>({})

  const modifyEmployee = ref<any>({})
  const modifyStore = ref<any>({})
  const modifyStoreType = ref<string>('new')

  const fileType40ImageList = ref<any[]>([])
  const fileType41ImageList = ref<any[]>([])
  const fileType42ImageList = ref<any[]>([])
  function resetImageList() {
    fileType40ImageList.value = []
    fileType41ImageList.value = []
    fileType42ImageList.value = []
  }

  function dealImageList(info: any) {
    // 找门头照
    fileType40ImageList.value = info.imageList.filter((x: any) => x.fileType === '40').map(img => Object.assign({
      name: img.imageId,
      url: '',
      status: 'ready',
      message: '上传成功',
      type: 'image',
    }))
    // 找经营照
    fileType41ImageList.value = info.imageList.filter((x: any) => x.fileType === '41').map(img => Object.assign({
      name: img.imageId,
      url: '',
      status: 'ready',
      message: '上传成功',
      type: 'image',
    }))
    // 找佐证材料
    fileType42ImageList.value = info.imageList.filter((x: any) => x.fileType === '42').map(img => Object.assign({
      name: img.imageId,
      url: '',
      status: 'ready',
      message: '上传成功',
      type: 'image',
    }))
  }

  // 查询门店
  async function getStoreInfo(storeId: string) {
    console.log('getStoreInfo')
    console.log('storeId')
    console.log(storeId)
    const { data } = await mcht.getStoreInfo(storeId)
    if (data.status === 'success') {
      modifyStore.value = data.data
      if (modifyStore.value.imageList) {
        dealImageList(modifyStore.value)
      }
      else {
        resetImageList()
      }
    }
  }

  return {
    mchtNo,
    shopId,
    userName,
    roleFlag,
    notice,
    phoneNumber,
    digitalCurrency,
    discount,
    transactionSummary,
    modifyEmployee,
    modifyStore,
    modifyStoreType,
    msgSwitch,
    fileType40ImageList,
    fileType41ImageList,
    fileType42ImageList,
    menuBit,
    bit8,
    bit10,
    adjustFlag,
    sb,
    resetImageList,
    modifyRefund,
    getStoreInfo,
  }
})
